﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockCrawlerLib.DAL;
using DevExpress.Web;
using DevExpress.Data;
using System.Text.RegularExpressions;
using CrawlerController;
using System.Net;

namespace StockCrawler {
    public partial class _Default : System.Web.UI.Page {
        private StockRepository _stockRepo = new StockRepository();
        private HistoryRepository _hisRepo = new HistoryRepository();
        protected void Page_Load(object sender, EventArgs e)
        {
            //DateTime dt = new DateTime(2016, 11, 4);
            //FptsCrawler2 obj = new FptsCrawler2();
            //obj.ImportPriceHisotry("", dt);
            //obj.ImportOrderHisotry("", dt);
            //obj.ImportForeignHistory(dt);
            //obj.ImportForeign2History(dt);

            if (!this.IsPostBack)
                objUcStockFilter.StockFilterEvent += new EventHandler(EventHandlerFunction_StockFilter);
            else
            {
                gridViewStock.DataSource = (IQueryable)Session["StockFilterResult"];
                gridViewStock.PageIndex = 0;
                gridViewStock.DataBind();
            }
        }

        

        public void EventHandlerFunction_StockFilter(object sender, EventArgs e)
        {
            var dt = _hisRepo.Select2(190);
            objUcHistoryInfo.UpdateHistory(dt);
            //objUcHistoryInfo.UpdateHistory(null);
            gridViewStock.DataSource = (IQueryable)Session["StockFilterResult"];
            gridViewStock.PageIndex = 0;
            gridViewStock.DataBind();
        }

        
        protected void chkIsRemark_CheckedChanged(object sender, EventArgs e)
        {
            ASPxCheckBox chk = (ASPxCheckBox)sender;
            //"cell4_1"
            Regex r = new Regex("cell(?<value>[0-9]+)_[0-9]+");
            Match m = r.Match(chk.Parent.ID);
            int rowIndex = int.Parse(m.Result("${value}"));

            int id_stock = (int)gridViewStock.GetRowValues(rowIndex, new string[] { "id_stock" });
            if (id_stock > 0)
            {
                Session["id_stock"] = id_stock;
                string code = _stockRepo.Select(id_stock).code;
                _stockRepo.UpdateIsRemark(id_stock, chk.Checked);
            }
        }

        protected void ASPxCallbackPanelHistory_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e)
        {
            try
            {
                string[] args = e.Parameter.Split('|');
                if (args == null || args.Length == 0)
                    return;

                if (gridViewStock.VisibleRowCount > 0)
                {
                    if (args[0].Equals("INDEX"))
                    {
                        int id_stock = (int)gridViewStock.GetRowValues(Convert.ToInt32(args[1]), new string[] { "id_stock" });
                        Session["id_stock"] = id_stock;

                        CafefCrawler cafef = new CafefCrawler();
                        string code = gridViewStock.GetRowValues(Convert.ToInt32(args[1]), new string[] { "code" }).ToString();
                        pricebox.Controls.AddAt(0, new LiteralControl(cafef.GetCurrentInfo(code)));

                        var dt = _hisRepo.Select2(id_stock);
                        objUcHistoryInfo.UpdateHistory(dt);
                    }
                    else if (args[0].Equals("SORT"))
                    {
                        int id_stock = (int)Session["id_stock"];

                        var dt = _hisRepo.Select2(id_stock);
                        objUcHistoryInfo.UpdateHistory(dt);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        
    }
}